package com.bawei.module_mine.model

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.util.regex.Pattern

open class GetPermissions {

    companion object {
        private const val PERMISSION_REQUEST_CODE = 100
        
        // 检查联系人/通讯录权限
        fun hasContactsPermission(context: Context): Boolean {
            return ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.READ_CONTACTS
            ) == PackageManager.PERMISSION_GRANTED
        }
        
        // 检查相机权限
        fun hasCameraPermission(context: Context): Boolean {
            return ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.CAMERA
            ) == PackageManager.PERMISSION_GRANTED
        }
        
        // 检查录音权限
        fun hasAudioPermission(context: Context): Boolean {
            return ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.RECORD_AUDIO
            ) == PackageManager.PERMISSION_GRANTED
        }
        
        // 检查存储权限
        fun hasStoragePermission(context: Context): Boolean {
            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                // Android 13及以上使用新的权限
                ContextCompat.checkSelfPermission(
                    context,
                    Manifest.permission.READ_MEDIA_IMAGES
                ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(
                    context,
                    Manifest.permission.READ_MEDIA_VIDEO
                ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(
                    context,
                    Manifest.permission.READ_MEDIA_AUDIO
                ) == PackageManager.PERMISSION_GRANTED
            } else {
                ContextCompat.checkSelfPermission(
                    context,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_GRANTED
            }
        }
        
        // 检查所有必要权限
        fun hasAllPermissions(context: Context): Boolean {
            return hasContactsPermission(context) && 
                   hasCameraPermission(context) && 
                   hasAudioPermission(context) && 
                   hasStoragePermission(context)
        }
        
        // 请求联系人权限
        fun requestContactsPermission(activity: Activity) {
            if (!hasContactsPermission(activity)) {
                ActivityCompat.requestPermissions(
                    activity,
                    arrayOf(Manifest.permission.READ_CONTACTS),
                    PERMISSION_REQUEST_CODE
                )
            }
        }
        
        // 请求所有必要权限
        fun requestAllPermissions(activity: Activity) {
            val permissions = mutableListOf<String>()
            
            if (!hasContactsPermission(activity)) {
                permissions.add(Manifest.permission.READ_CONTACTS)
            }
            if (!hasCameraPermission(activity)) {
                permissions.add(Manifest.permission.CAMERA)
            }
            if (!hasAudioPermission(activity)) {
                permissions.add(Manifest.permission.RECORD_AUDIO)
            }
            if (!hasStoragePermission(activity)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    permissions.add(Manifest.permission.READ_MEDIA_IMAGES)
                    permissions.add(Manifest.permission.READ_MEDIA_VIDEO)
                    permissions.add(Manifest.permission.READ_MEDIA_AUDIO)
                } else {
                    permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
                }
            }
            
            if (permissions.isNotEmpty()) {
                ActivityCompat.requestPermissions(
                    activity,
                    permissions.toTypedArray(),
                    PERMISSION_REQUEST_CODE
                )
            }
        }
        
        // 正则匹配手机号
        fun regexPhoneNumber(phoneNumber: String): Boolean {
            val regex = "^1[3-9]\\d{9}$"
            val pattern = Pattern.compile(regex)
            return pattern.matcher(phoneNumber).matches()
        }
        
        fun desensitizePhoneNumber(phoneNumber: String): String {
            return if (phoneNumber.length == 11) {
                val prefix = phoneNumber.substring(0, 3)
                val suffix = phoneNumber.substring(phoneNumber.length - 4)
                "$prefix****$suffix"
            } else {
                "无效手机号"
            }
        }
    }
}





































